#include "DeviceItemWidget.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QEnterEvent>
#include <QSizePolicy>
#include <QMouseEvent>
#include <QDebug>

/* 当前唯一激活项 */
static DeviceItemWidget *g_currentActive = nullptr;

/* ===== 构造 ===== */
DeviceItemWidget::DeviceItemWidget(const QString& name,
                                   const QString& status,
                                   const QString& idText,
                                   const QColor& statusColor,
                                   QWidget *parent)
    : QWidget(parent), dotColor(statusColor)
{
    setObjectName("deviceCard");
    setAttribute(Qt::WA_Hover, true);
    setAttribute(Qt::WA_StyledBackground, true);
    setCursor(Qt::PointingHandCursor);

    auto *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setBlurRadius(15);
    shadow->setOffset(0,0);
    shadow->setColor(QColor(0,0,0,50));
    setGraphicsEffect(shadow);

    auto *root = new QHBoxLayout(this);
    root->setContentsMargins(4,4,4,4);
    root->setSpacing(0);

    leftLine = new QFrame(this);
    leftLine->setFixedWidth(4);

    nameLabel = new QLabel(name,this);
    nameLabel->setStyleSheet(
        "color:white;font-family:'Microsoft YaHei';font-weight:600;font-size:16px;background:transparent;");

    statusDot = new QLabel(this);
    statusDot->setFixedSize(10,10);
    statusDot->setStyleSheet(
        QString("background-color:%1;border-radius:5px;").arg(dotColor.name()));

    statusLabel = new QLabel(status,this);
    statusLabel->setStyleSheet(
        "color:white;font-family:'Microsoft YaHei';font-size:14px;background:transparent;");
    statusLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Preferred);

    idLabel = new QLabel(idText,this);
    idLabel->setStyleSheet(
        "color:white;font-family:'Microsoft YaHei';font-weight:600;font-size:17px;background:transparent;");
    idLabel->setAlignment(Qt::AlignRight|Qt::AlignTop);

    auto *stsLay = new QHBoxLayout;
    stsLay->addWidget(statusDot);
    stsLay->addSpacing(8);
    stsLay->addWidget(statusLabel);
    stsLay->setContentsMargins(0,0,0,0);

    auto *leftLay = new QVBoxLayout;
    leftLay->addWidget(nameLabel);
    leftLay->addLayout(stsLay);
    leftLay->setSpacing(8);

    auto *cntLay = new QHBoxLayout;
    cntLay->addLayout(leftLay);
    cntLay->addStretch();
    cntLay->addWidget(idLabel);
    cntLay->setContentsMargins(16,16,16,16);

    root->addWidget(leftLine);
    root->addLayout(cntLay);

    updateStyle();
}

/* ===== 析构 ===== */
DeviceItemWidget::~DeviceItemWidget()
{
    if (g_currentActive == this) {
        g_currentActive = nullptr;
    }
}

/* ===== 交互 ===== */
void DeviceItemWidget::enterEvent(QEnterEvent*)
{
    if (!isVisible()) return;
    isHovered = true;
    updateStyle();
}

void DeviceItemWidget::leaveEvent(QEvent*)
{
    if (!isVisible()) return;
    isHovered = false;
    updateStyle();
}

void DeviceItemWidget::mousePressEvent(QMouseEvent *e)
{
    if (!isVisible() || !isEnabled())
        return;

    if (g_currentActive && g_currentActive != this) {
        if (!g_currentActive->isVisible())
            g_currentActive = nullptr;
        else
            g_currentActive->setActive(false);
    }

    setActive(true);
    g_currentActive = this;

    emit clicked(this);
    QWidget::mousePressEvent(e);
}

/* ===== setter ===== */
void DeviceItemWidget::setActive(bool a)
{
    if (!isVisible()) return;
    isActive = a;
    updateStyle();
}

bool DeviceItemWidget::isActiveItem() const { return isActive; }

void DeviceItemWidget::setName(const QString& s)       { nameLabel->setText(s); }
void DeviceItemWidget::setStatusText(const QString& s) { statusLabel->setText(s); }
void DeviceItemWidget::setDeviceId(const QString& s)   { idLabel->setText(s); }

void DeviceItemWidget::setStatusColor(const QColor& c)
{
    dotColor = c;
    statusDot->setStyleSheet(
        QString("background-color:%1;border-radius:5px;").arg(c.name()));
}

/* ===== 样式 ===== */
void DeviceItemWidget::updateStyle()
{
    if (!isVisible()) return;

    const QString activeBarGradient = "qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #36F49E,stop:1 #17D47F)";
    const QString normalBar = "#17a2b8";

    QString bg  = "#1E293B";
    QString bar = normalBar;
    QString borderColor = "#3F4E62";
    int borderW = 1;

    if (isActive) {
        bg = "#083B34";
        bar = activeBarGradient;
    } else if (isHovered) {
        bg = "#112B44";
        borderColor = "#5ED4AF";
    }

    setStyleSheet(QString(R"(
        #deviceCard{
            background-color:%1;
            border:%2px solid %3;
            border-radius:12px;
            font-family:'Microsoft YaHei';
        })").arg(bg).arg(borderW).arg(borderColor));

    leftLine->setStyleSheet(QString(R"(
        background:%1;
        border-top-left-radius:12px;
        border-bottom-left-radius:12px;
    )").arg(bar));
}
